package com.ougy.reshop.action.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.ougy.reshop.action.BaseAction;
import com.ougy.reshop.model.Accessory;
import com.ougy.reshop.model.Album;
import com.ougy.reshop.util.FileExt;

public class BaseUploadAction extends BaseAction {
	private Logger logger=Logger.getLogger(getClass());
	/**
	 * 
	 */
	private static final long serialVersionUID = -4524009904133272485L;
	protected List<File> upload;
	protected List<String> uploadContentType;
	protected String savePath;
	protected List<String> uploadFileName;
	protected List<String> oldFileName;
	protected List<String> fextname;

	protected Accessory upload(int i) {
		Accessory accessory = null;
		if (upload != null) {
			accessory = new Accessory();
			String tFilePath = ServletActionContext.getServletContext()
					.getRealPath(getSavePath());
			File test = new File(tFilePath);
			if (!test.exists()) {
				test.mkdirs();
			}
			try {
				FileOutputStream fos = new FileOutputStream(tFilePath + "/"
						+ getUploadFileName().get(i));
				FileInputStream fis = new FileInputStream(getUpload().get(i));
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
				fos.close();
				fis.close();

			} catch (Exception e) {
				e.printStackTrace();
			}
			accessory.setName(getUploadFileName().get(i));
			accessory.setExt(fextname.get(i));
			accessory.setPath(getSavePath());
		}

		return accessory;
	}
	
	protected Album uploadMore(){
		Album album=null;
		if(upload!=null){
			album=new Album();
			for(int i=0;i<upload.size();i++){
				Accessory accessory=upload(i);
				accessory.setAlbum(album);
				album.getPhotos().add(accessory);
				sAlbum.add(album);
			}
		}
		return album;
	}

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getSavePath() {
		return savePath;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		oldFileName=new ArrayList<String>();
		fextname=new ArrayList<String>();
		this.uploadFileName=new ArrayList<String>();
		for(int i=0;i<uploadFileName.size();i++){
			String tempName=uploadFileName.get(i);
			oldFileName.add(i, tempName);
			String id = getUuid().substring(1, 5);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssS");
			String date = sdf.format(new Date());
			fextname.add(i,FileExt.getExtension(tempName));
			this.uploadFileName.add(i,date + id + "." + FileExt
					.getExtension(tempName));
		}
	}

	public void setSavePath(String savePath) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String date = sdf.format(new Date());
		this.savePath = (savePath + "/" + date);
	}
	public List<String> getOldFileName() {
		return this.oldFileName;
	}
	public void setOldFileName(List<String> oldFileName) {
		this.oldFileName = oldFileName;
	}
	public List<String> getFextname() {
		return this.fextname;
	}

	public void setFextname(List<String> fextname) {
		this.fextname = fextname;
	}
	
}
